package com.alibaba.cirtus.ots.application.adapter.ability.model.enums;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 业务单元枚举
 *
 * @author wxy
 * @since 2024/1/24
 */
public enum BusinessUnitEnum {
    // 销售组织、业务渠道、客户组 配置为null表示不校验
    // def 设置为true表示没有匹配到则返回该业务单元
    UNIT_1171("1171", "电商", Lists.newArrayList(SalesOrgEnum.ORG_1171), null, null, false),
    UNIT_6140("6140", "特通", Lists.newArrayList(SalesOrgEnum.ORG_6140), Lists.newArrayList(SapChannelEnum.CHANNEL_A3), null, false),
    UNIT_6140_A1("6140A1", "KA", Lists.newArrayList(SalesOrgEnum.ORG_6140), Lists.newArrayList(SapChannelEnum.CHANNEL_A1), null, false),
    UNIT_6000_20("600020", "分仓", Lists.newArrayList(SalesOrgEnum.ORG_6000), Lists.newArrayList(SapChannelEnum.CHANNEL_20), Lists.newArrayList(CustomerGroupEnum.GROUP_01), false),
    UNIT_6000("6000", "常温总部", Lists.newArrayList(SalesOrgEnum.ORG_6000), Lists.newArrayList(SapChannelEnum.CHANNEL_20), Lists.newArrayList(CustomerGroupEnum.GROUP_02), false),
    UNIT_6136("6136", "湖北蒙牛乳业销售有限公司", null, null, null, true),
    ;

    public static void main(String[] args) {
        System.out.println(matchBusinessUnit("6140","A1","02").getCode());
    }

    public static BusinessUnitEnum matchBusinessUnit(String salesOrgCode, String sapChannel, String customerGroup) {
        return Arrays.stream(values()).filter(r -> !r.def).filter(r -> {
            boolean matchSalesOrg;
            if (r.getSalesOrgEnums() == null) {
                matchSalesOrg = true;
            } else {
                matchSalesOrg = r.getSalesOrgEnums().stream().anyMatch(
                    org -> StringUtils.equals(org.getCode(), salesOrgCode));
            }
            boolean matchSapChannel;
            if (r.getSapChannelEnums() == null) {
                matchSapChannel = true;
            } else {
                matchSapChannel = r.getSapChannelEnums().stream().anyMatch(
                    org -> StringUtils.equals(org.getCode(), sapChannel));
            }
            boolean matchCustomerGroup;
            if (r.getCustomerGroupEnums() == null) {
                matchCustomerGroup = true;
            } else {
                matchCustomerGroup = r.getCustomerGroupEnums().stream().anyMatch(
                    org -> StringUtils.equals(org.getCode(), customerGroup));
            }
            return matchSalesOrg && matchSapChannel && matchCustomerGroup;
        }).findFirst().orElse(Arrays.stream(values()).filter(r -> r.def).findFirst().orElse(UNIT_6136));
    }

    private String code;

    private String name;

    private List<SalesOrgEnum> salesOrgEnums;

    private List<SapChannelEnum> sapChannelEnums;

    private List<CustomerGroupEnum> customerGroupEnums;

    private boolean def;

    BusinessUnitEnum(String code, String name, List<SalesOrgEnum> salesOrgEnums, List<SapChannelEnum> sapChannelEnums,
        List<CustomerGroupEnum> customerGroupEnums, boolean def) {
        this.code = code;
        this.name = name;
        this.salesOrgEnums = salesOrgEnums;
        this.sapChannelEnums = sapChannelEnums;
        this.customerGroupEnums = customerGroupEnums;
        this.def = def;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<SalesOrgEnum> getSalesOrgEnums() {
        return salesOrgEnums;
    }

    public void setSalesOrgEnums(List<SalesOrgEnum> salesOrgEnums) {
        this.salesOrgEnums = salesOrgEnums;
    }

    public List<SapChannelEnum> getSapChannelEnums() {
        return sapChannelEnums;
    }

    public void setSapChannelEnums(List<SapChannelEnum> sapChannelEnums) {
        this.sapChannelEnums = sapChannelEnums;
    }

    public List<CustomerGroupEnum> getCustomerGroupEnums() {
        return customerGroupEnums;
    }

    public void setCustomerGroupEnums(List<CustomerGroupEnum> customerGroupEnums) {
        this.customerGroupEnums = customerGroupEnums;
    }

    public boolean isDef() {
        return def;
    }

    public void setDef(boolean def) {
        this.def = def;
    }
}
